<?php

/**
 * This is the model class for table "portal_users".
 *
 * The followings are the available columns in table 'portal_users':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property integer $is_super
 * @property string $last_activity
 * @property string $last_ipv4
 * @property string $created_date
 */
class PortalUsers extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return PortalUsers the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'portal_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username', 'required'
                            ,'message'=>'Thiếu tên truy cập'),
                        array(
                            'password','required','message'=>'Thiếu mật khẩu'
                        ),
			array('is_super', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>20),
			array('password, last_ipv4', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, is_super, last_activity, last_ipv4, created_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Tên truy cập',
			'password' => 'Mật khẩu',
			'is_super' => 'Is Super',
			'last_activity' => 'Last Activity',
			'last_ipv4' => 'Last Ipv4',
			'created_date' => 'Created Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('is_super',$this->is_super);
		$criteria->compare('last_activity',$this->last_activity,true);
		$criteria->compare('last_ipv4',$this->last_ipv4,true);
		$criteria->compare('created_date',$this->created_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public static function getUserNameById($id){
            $model = PortalUsers::model()->findByPk($id);
            return $model->username;
        }
}